﻿/*
知识结构的添加修改
*/
//
angular.module('knowledgeStructureAddEdit', ['vModal','loadModule', 'knowledgeSelector', 'PensonSelector','modelIcon'])
 .directive("knowledgeStructureAddEdit", ["$rootScope", "$stateParams", "$timeout", function ($rootScope, $stateParams, $timeout) {
     return {
         restrice: "AE",
         scope: {
             isSaveBtn: "=", // 是否正在请求中
             kssList:"=",   // 父节点对象
             isHide: "=",    //是否显示出来这个内容 
             knowledgeStructure: "=", //单独的二级运营对象
             onSave: "&"      //保存对象到服务器                     
         },
         templateUrl: "/system/directive/train/knowledgestructure/knowledge_structure_add_edit.html",
         link: function (scope, element, attr) {
             scope.isHide = false;
             //关闭窗口
             scope.isHideFun = function () {   
                //scope.kssList[0] = {};             
                scope.isSaveBtn = false;  //关闭loading
                scope.isHide = false;
             }
             
             scope.checkedStructureShow=function(){
                if(scope.knowledgeStructure.display==undefined){
                    scope.knowledgeStructure.display=false;
                 }
                scope.knowledgeStructure.display=!scope.knowledgeStructure.display;
             }
             scope.mousePosition = function (ev) {
                if (!ev) {
                    ev = window.event;
                }

                if (ev.type != "click") {
                    return;
                }
                var width = 0;
                var height = 0;
                if (ev.pageX || ev.pageY) {//firefox、chrome等浏览器
                    if (document.documentElement.clientWidth - ev.pageX < 450) {
                        width = ev.pageX - (450 - (document.documentElement.clientWidth - ev.pageX)) - 20;
                    } else {
                        width = ev.pageX;
                    }
                    if (document.documentElement.clientHeight - ev.pageY < 480) {
                        height = ev.pageY - (480 - (document.documentElement.clientHeight - ev.pageY)) - 20;
                    } else {
                        height = ev.pageY;
                    }

                    return { "left": width + "px", "top": height + "px", "position": "fixed" };
                }

                return {// IE浏览器
                    "left": ev.clientX + document.body.scrollLeft - document.body.clientLeft,
                    "top": ev.clientY + document.body.scrollTop - document.body.clientTop
                };
            }
             // 人员选择器
             //批量添加负责人 选择器初始化
             scope.personData = {

                 ApiOpen: false,
                 style: "",
                 menu: [{ "id": "User"}],
                 UserList: [],
                 ViceuserOrgList: [],
                 ViceuserRoleList: [],
                 ViceuserGroupList: [],
                 searchType: [],  //初始化教师查询
//                 searchTexts: 3,  // 初始化教师查询
                 DB: "0",
                 DBdata: []

             }
             //监听负责人列表事件
             scope.WatchpersonDataList = function () {
                 var watch = scope.$watch('personData.ApiOpen', function (newValue, oldValue, scope) {
                     scope.knowledgeStructure.ulist = scope.personData.UserList;
                 }, true);
             }
             // 新增负责人
             scope.showPersonSelect = function (event) {
                  scope.personData.style = scope.mousePosition(event);
                  scope.personData.UserList=[];               
                     angular.forEach(scope.knowledgeStructure.ulist,function(data,index,array){
                          //将负责人集合 进行双向绑定
                            var has = false;
                            //去重业务
                            scope.personData.UserList.forEach(function (child, childindex) {
                                //判断是否已存在 
                            if (child.user_ID == data.user_ID) {
                                has = true;
                                return;
                            }
                        });
                        //如果不存在 则添加
                        if (!has) {
                            scope.personData.UserList.push(data);
                        }                  
                    });
                 scope.personData.ApiOpen = true;
                 scope.WatchpersonDataList();
             }
             //保存知识结构
             scope.btnSave = function () {                    
                try{
                    scope.knowledgeStructure.parentId =   scope.kssList[0].knowledge_Id;            // 知识结构父级id
                    scope.knowledgeStructure.parentName = scope.kssList[0].knowledge_Name;   // 知识结构父级名称
                    //顶级节点 这个也是为了更新编辑时选中的父节点的级层数是负数
                    if(scope.knowledgeStructure.parentId == 0 || scope.knowledgeStructure.parentId == undefined || scope.knowledgeStructure.parentId == "" || scope.knowledgeStructure.parentId == null){ 
                        scope.knowledgeStructure.classification = 0;
                    }else{  // 非顶级节点需要选中节点
                        scope.knowledgeStructure.classification = parseInt(scope.kssList[0].classification);    //节点级数
                    }                     
                }catch(e){
                    //console.log("没有选择任何知识结构，默认为父节点0");
                    scope.knowledgeStructure.parentId = 0;          // 知识结构id
                    scope.knowledgeStructure.parentName = '父节点';
                    scope.knowledgeStructure.classification = 0;    //节点级数
                }                                      
                 scope.onSave();
             }
             /******************修改知识结构父级**********************/             
             //配置知识选择器
             scope.knowConfig = {   // 配置知识选择器的参数
                 menu: ["structure"],
                 kssMore:false, //知识结构 是否多选
                 kssList:[],    // 知识结构选中列表
                 isShow:false,  // 显示知识选择器
             };                           
             //scope.knowConfig.kssList = scope.kssList;
             // 知识选择器的回调
            scope.onCallback = function(){
                scope.kssList  = scope.knowConfig.kssList;                 
            }
             // 打开知识选择器
             scope.modifyOrder = function () {
                 // 新增子节点 需要重新赋值对象
                 scope.knowConfig.kssList = scope.kssList;
                 scope.knowConfig.isShow = true;
             }
             /*** 图标选择器*****/
             scope.isIconShow = false;        
             scope.openIcon = function(){
                scope.isIconShow = true;                  
             }

             scope.closeIcon = function(icon){               
                scope.isIconShow = false;                   
             }

         },
         controller: function ($scope, $document) {
             //点击非指定区域，关闭显示层
             $document.bind("click", function (evt) {
                 var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                 if ($document[0].getElementById("erjiyunying_fenpeiqiye_middle") != null) {
                     var bt = $document[0].getElementById("erjiyunying_fenpeiqiye_middle").contains(target);
                     if (!bt) {
                         $scope.$apply(function () {
                             $scope.isOrgFlag = false; // 企业搜索列表框
                         });
                     }
                 }
             });
         }
     }
 } ]);
